package com.ruoyi.collect.controller;

import com.ruoyi.collect.domain.CollectBind;
import com.ruoyi.collect.service.ICollectBindService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 采集类型绑定控制器
 *
 * @author ruoyi
 * @date 2025-11-07
 */
@RestController
@RequestMapping("/collect/bind")
public class CollectBindController extends BaseController {

    @Autowired
    private ICollectBindService collectBindService;

    /**
     * 查询采集绑定列表
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:list')")
    @GetMapping("/list")
    public TableDataInfo list(CollectBind collectBind) {
        startPage();
        List<CollectBind> list = collectBindService.selectCollectBindList(collectBind);
        return getDataTable(list);
    }

    /**
     * 导出采集绑定列表
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:export')")
    @Log(title = "采集绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CollectBind collectBind) {
        List<CollectBind> list = collectBindService.selectCollectBindList(collectBind);
        ExcelUtil<CollectBind> util = new ExcelUtil<CollectBind>(CollectBind.class);
        util.exportExcel(response, list, "采集绑定数据");
    }

    /**
     * 获取采集绑定详细信息
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:query')")
    @GetMapping(value = "/{bindId}")
    public AjaxResult getInfo(@PathVariable("bindId") Long bindId) {
        return AjaxResult.success(collectBindService.selectCollectBindById(bindId));
    }

    /**
     * 新增采集绑定
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:add')")
    @Log(title = "采集绑定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CollectBind collectBind) {
        return toAjax(collectBindService.insertCollectBind(collectBind));
    }

    /**
     * 修改采集绑定
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:edit')")
    @Log(title = "采集绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CollectBind collectBind) {
        return toAjax(collectBindService.updateCollectBind(collectBind));
    }

    /**
     * 删除采集绑定
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:remove')")
    @Log(title = "采集绑定", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bindIds}")
    public AjaxResult remove(@PathVariable Long[] bindIds) {
        return toAjax(collectBindService.deleteCollectBindByIds(bindIds));
    }

    /**
     * 根据站点获取绑定
     */
    @GetMapping("/site/{siteId}")
    public AjaxResult getBindsBySite(@PathVariable Long siteId) {
        List<CollectBind> binds = collectBindService.selectBindsBySiteId(siteId);
        return AjaxResult.success(binds);
    }

    /**
     * 从远程站点自动绑定类型
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:auto')")
    @PostMapping("/auto/{siteId}")
    public AjaxResult autoBindTypes(@PathVariable Long siteId) {
        int count = collectBindService.autoBindTypes(siteId);
        return AjaxResult.success("已创建 " + count + " 个类型绑定");
    }

    /**
     * 清除站点绑定
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:clear')")
    @DeleteMapping("/clear/{siteId}")
    public AjaxResult clearBindings(@PathVariable Long siteId) {
        int count = collectBindService.clearSiteBindings(siteId);
        return AjaxResult.success("已删除 " + count + " 个绑定");
    }

    /**
     * 同步分类列表
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:sync')")
    @Log(title = "同步分类", businessType = BusinessType.UPDATE)
    @PostMapping("/syncTypes/{siteId}")
    public AjaxResult syncTypes(@PathVariable Long siteId) {
        java.util.Map<String, Object> result = collectBindService.syncTypeList(siteId);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return AjaxResult.success((String) result.get("message"), result);
        } else {
            return AjaxResult.error((String) result.get("message"));
        }
    }

    /**
     * 获取绑定配置页面数据
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:query')")
    @GetMapping("/config/{siteId}")
    public AjaxResult getBindConfig(@PathVariable Long siteId) {
        List<CollectBind> binds = collectBindService.selectBindsBySiteId(siteId);
        java.util.Map<String, Object> config = new java.util.HashMap<>();
        config.put("binds", binds);
        return AjaxResult.success(config);
    }

    /**
     * 保存绑定配置
     */
    @PreAuthorize("@ss.hasPermi('collect:bind:edit')")
    @Log(title = "保存绑定配置", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    public AjaxResult saveBinds(@RequestBody List<CollectBind> binds) {
        int count = collectBindService.batchBindTypes(binds);
        return AjaxResult.success("成功保存 " + count + " 个绑定配置");
    }
}